<?php
		namespace App\Http\Controllers\Admin;
		use App\Http\Controllers\Controller;
		use \Illuminate\Http\Request;
		use Session;
		use DB;
		use Crypt;
		class AdminController extends Controller{
				/*
					我在这里写一个构造方法控制判断是否登录 
					如果没有就跳转到登录页面
					后台必须先登录否则容易泄露用户信息
				*/
				public function __construct(){
					if(!Session::get('u_name')){
						//echo "对不起你还没有登录请先<a href='admin/login'>登录</a>";die;
						//return redirect("admin/login");die;
						header("Location:admin/login");die;
					}
				}
				public function index(Request $request){
                    $rid = session('r_id');
					return view('admin.index',array('num'=>$rid));
				}
				public function qxgl(){
					return view('admin.qxgl');
			    }
			    //管理员列表
				public function gly_list(){
					$data = DB::table('mo_huser')->where('u_status','1')->get();
					//var_dump($data);die;
					return view('admin.gly_list',array('data'=>$data));
			    }
			    //渲染管理员添加页面
			    public function gly_add(){
			    	return view('admin.gly_add');
			    }
			    //添加管理员
			    public function guan_add(Request $requset){
			    	//自动验证
		           	$this->validate($requset, [
						'u_name'		=>'required|max:30|unique:mo_huser',
						'u_password'	=>'required|max:10',
						'u_sex'			=>'required',
						'u_phone'		=>'required|max:11',
						'u_email'		=>'required',
						'u_remarks'		=>'required|max:100'
					],
					[
						'u_name.required' 		=> '用户名必须填写11111',
						'u_name.max'			=> '用户名最多填写30个字符（一个中文占用两个字符）',
						'u_name.unique'			=> '用户名已注册请重新填写',
						'u_password.required'	=> '密码必须填写',
						'u_sex.required'		=> '性别必选',
						'u_phone.required'		=> '手机号必填',
						'u_phone.max'			=> '手机号格式错误',
						'u_email.required'		=> '邮箱必填',
						'u_remarks.required'	=> '你还没有介绍自己'
					]
					);

		           	$name = $requset->input('u_name');
		           	$pass = Crypt::encrypt($requset->input('u_password'));

		           	$sex  = $requset->input('u_sex');
		           	$phone= $requset->input('u_phone');
		           	$email= $requset->input('u_email');
		           	$remar= $requset->input('u_remarks');
		           	$start= time();
		           	$bool = DB::table('mo_huser')->insert([
		           			'u_name'		=>$name,
		           			'u_password'	=>$pass,
		           			'u_sex'			=>$sex,
		           			'u_phone'		=>$phone,
		           			'u_email'		=>$email,
		           			'u_remarks'		=>$remar,
		           			'u_starttime'	=>$start
		           		]);
		           	if($bool == true){
		           		return redirect('admin/gly_list');
		           	}else{
		           		return redirect()->back();
		           	}
			    }
			    //删除管理员
			    public function gly_del($id){
		    		if(DB::update("update mo_huser set u_status = 0 where id=".$id)){
		    			return redirect('admin/gly_list');
			    	}else{
			    		return redirect()->back()->withInput()->withErrors('删除失败');
			    	}	
			    }
			    //管理员的个人信息
				public function person_info(){
					//从session里把登录用户取出来
					$name = Session::get('u_name');
					$data = DB::table('mo_huser')->where('u_name','=',$name)->first();
					return view('admin.person_info',array('data'=>$data));
			    }
			    //修改管理员信息
			    public function gly_up(Request $requset){
			    	$name = $requset->input('u_name');
			    	$sex  = $requset->input('u_sex');
			    	$phone= $requset->input('u_phone');
			    	$email= $requset->input('u_email');
			    	$id = $requset->input('id');
			    	
			    	$data = DB::update("update mo_huser set u_name='".$name."',u_sex='".$sex."',u_phone='".$phone."',u_email='".$email."' where id=".$id);
			    	if($data == true){
			    		return redirect('admin/person_info');
			    	}else{
			    		return redirect()->back()->withInput()->withErrors('修改失败');
			    	}
			    }
			    //修改管理员密码
			    public function gly_pass(Request $requset){
			    	//接收传递过来的值
			    	$out  = $requset->input('outpassword');//旧密码
			    	$pass = $requset->input('password');//新密码
			    	$on	  = $requset->input('onpassword');//确认新密码
			    	//取出存在session里的name名
			    	$name = Session::get('u_name');
			    	$data = DB::table('mo_huser')->where('u_name','=',$name)->first();
			    	$row = Crypt::decrypt($data->u_password);
			    	if($out == $row){
			    		if($pass == $on){
			    			$word = Crypt::encrypt($pass);
			    			$bool = DB::update("update mo_huser set u_password='".$word."' where id=".$data->id);
			    			if($bool == true){
			    				$requset->session()->pull('u_name',session('u_name'));
			    				/*$title = array('content'=>'密码修改成功，请重新登录','url'=>'login');
			    				return view('admin.alert',['data'=>$title]);*/
			    				echo "<script>alert('密码修改成功，请重新登录!');parent.location.href='login';</script>"; 
			    				//return redirect('admin/person_info');
			    				/*return redirect('admin/login'); */
			    			}else{
			    				return redirect()->back();
			    			}
			    		}else{
			    			$title = array('content'=>'两次密码输入不一致请从新输入','url'=>'person_info');
			    			return view('admin.alert',['data'=>$title]);
			    		}
			    	}else{
			    		$title = array('content'=>'原密码错误 请从新输入','url'=>'person_info');
			    		return view('admin.alert',['data'=>$title]);
			    	}
			    }

			}	    
	
?>
